﻿using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using GrewalCRM.BLL;
using GrewalCRM.Entities;
using GrewalCRM.Entities.Common;

public partial class UC_AddCallUC : System.Web.UI.UserControl
{
  CallLog_BLL callLogBll = new CallLog_BLL();
  CustomersMaster customer = null;
  CallLog call = null;

  public delegate void SavingCall(bool isSavedSucessfully,int savedCallID);
  public event SavingCall SavingCallHandler;

  #region Events
  protected void Page_Load(object sender, EventArgs e)
  {
    //Page.Form.DefaultButton = btnSave.UniqueID;
    if (!Page.IsPostBack)
    {
      PopulateDealer();
      PopulateACBrands();
      populateCallType();
      if (Mode == ViewMode.New)
        UCCallSatus.Enabled = false;

      lblCallDateValue.Text = DateTime.Now.ToShortDateString();
    }    
  }
  protected void Page_PreRender(object sender, EventArgs e)
  {
    
    if (UCCallSatus.CallStatusID == (int)GrewalCRM.Entities.Common.CallStatus.Closed)
    {
      foreach (Control ctl in pnlSaveCall.Controls)
      {
        if ((ctl is TextBox) || (ctl is DropDownList))
        {
          (ctl as WebControl).Enabled = false; ;
        }
              
      }
      lblPreviousComment.Visible = true;
      txtComment.Visible = !lblPreviousComment.Visible;

      btnSave.Enabled = false;

      UCCallSatus.Enabled = false;
      UCdpACPurchaseDate.Enabled = false;      
    }
    
    if (Mode == ViewMode.Edit)
    {
      btnSave.Visible = false;
      btnCancel.Visible = false;
    }
       
  }
  public void btnSave_Click(object sender, EventArgs e)
  {
    int result;

    
      customer = new CustomersMaster(txtCustomerName.Text, txtAddress.Text, txtZipCode.Text, txtPhoneNo.Text, txtMobNo1.Text, txtMobNo2.Text, MasterCRM.SessionUser);

      call = new CallLog(DateTime.Parse(lblCallDateValue.Text), new ReffCallType(int.Parse(ddlCallType.SelectedValue)),
                              new DealersMaster(int.Parse(ddlDealer.SelectedValue)), customer, null, new InstallersMaster(UCCallSatus.InstallerID), null,
                              txtComment.Text,null, new ReffStatus(UCCallSatus.CallStatusID),new ACBrands(int.Parse(ddlACBrands.SelectedValue)),
                              UCdpACPurchaseDate.Date, txtACModelNo.Text, txtACSerialNo.Text, null, null, null, null, null, null, null,null,null,null, 
                              MasterCRM.SessionUser);

      if (Mode == ViewMode.Edit)
      {
        customer.ID                 =   CustomerID;
        call.ID                     =   CallID;
        call.CallScheduledDate      =   UCCallSatus.ScheduledDate;
        call.CallCompletedDate      =   UCCallSatus.CompletedDate;
        call.InstallerCommisionCR   =   UCCallSatus.InstallerCommisionCR;
        call.InstallerMaterialCR    =   UCCallSatus.InstallerMaterialCR;
        call.InstallerAcDR          =   UCCallSatus.InstallerAcDR;
        call.DealerAcDR             =   UCCallSatus.DealerAcDR;
        call.DealerAcCR             =   UCCallSatus.DealerAcCR;
        call.IsTempClosed           =   UCCallSatus.IsTempClosed;
      }



      result = callLogBll.Save(call);
      bool isSavedSucessfully = false;  

      if (result == 2627)
      {
        isSavedSucessfully=false;
        lblNotification.Text = "Record failed to save. May be duplicate record. Check the AC serial No.";
        lblNotification.ForeColor = Color.Red;
      }
      else if (result != 0)
      {
        isSavedSucessfully=true;
        lblNotification.Text = "Record save sucessfully with the Call ID = " + result.ToString();
        lblNotification.ForeColor = Color.Green;      
      }


      if (SavingCallHandler != null)
      {
        SavingCallHandler(isSavedSucessfully,CallID);
      }
   

  }
  protected void ddlCallType_SelectedIndexChanged(object sender, EventArgs e)
  {
    ddlDealer.Enabled = !(int.Parse(ddlCallType.SelectedValue) > 1);

  }
  #endregion

  #region Methods  
  private ImasterCRM MasterCRM
  {
    get { return this.Page.Master as ImasterCRM; }
  }
  public void PopulateDealer()
  {
    ddlDealer.DataSource = MasterCRM.DealerCache;
    ddlDealer.DataTextField = "DealerCounterName";
    ddlDealer.DataValueField = "ID";
    ddlDealer.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlDealer,"Dealer" );
  }
  public void PopulateACBrands()
  {
    ddlACBrands.DataSource = MasterCRM.ACBrandsCache;
    ddlACBrands.DataTextField = "ACBrandName";
    ddlACBrands.DataValueField = "ID";
    ddlACBrands.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlACBrands, "ACBrand");
  }
  public void populateCallType()
  {   
    ddlCallType.DataSource = CommonUtil.GetEnumForBind(typeof(CallType));
    ddlCallType.DataTextField = "value";
    ddlCallType.DataValueField = "key";
    ddlCallType.DataBind();
    CommonUtil.AddDefaultItemToDDL(ddlCallType, "Call Type");
  } 
  #endregion

  #region Properties
  public ViewMode Mode
  {
    get;
    set;
  }
  public CallSatusUC StatusUC
  {
    get { return UCCallSatus; }
  }
  public int Installer
  {
    get { return UCCallSatus.InstallerID; }
    set { UCCallSatus.InstallerID = value; }
  }
  public int CallStatus
  {
    get { return UCCallSatus.CallStatusID; }
    set { UCCallSatus.CallStatusID = value; }
  }
  public DateTime? CallScheduledDate
  {
    get { return UCCallSatus.ScheduledDate; }
    set { UCCallSatus.ScheduledDate = value; }
  }

  public int CustomerID
  {
    get { return Convert.ToInt32(hdnCustomerID.Value); }
    set { hdnCustomerID.Value = value.ToString(); }
  }
  public string CustomerName
  {
    get { return txtCustomerName.Text; }
    set { txtCustomerName.Text = value; }
  }
  public string Address
  {
    get { return txtAddress.Text; }
    set { txtAddress.Text = value; }
  }
  public string ZipCode
  {
    get { return txtZipCode.Text; }
    set { txtZipCode.Text = value; }
  }
  public string PhoneNo
  {
    get { return txtPhoneNo.Text; }
    set { txtPhoneNo.Text = value; }
  }
  public string MobileNo1
  {
    get { return txtMobNo1.Text; }
    set { txtMobNo1.Text = value; }
  }
  public string MobileNo2
  {
    get { return txtMobNo2.Text; }
    set { txtMobNo2.Text = value; }
  }

  public int CallID
  {
    get { return Convert.ToInt32(hdnCallID.Value); }
    set { hdnCallID.Value = value.ToString(); }
  }
  public int CallType
  {
    get { return int.Parse(ddlCallType.SelectedValue); }
    set { ddlCallType.SelectedValue = value.ToString(); }
  }
  public int Dealer
  {
    get { return int.Parse(ddlDealer.SelectedValue); }
    set { ddlDealer.SelectedValue = value.ToString(); }
  }
  public string ACSerialNo
  {
    get { return txtACSerialNo.Text; }
    set { txtACSerialNo.Text = value; }
  }
  public DateTime? ACPurchasedDate
  {
    get { return UCdpACPurchaseDate.Date; }
    set { UCdpACPurchaseDate.Date = value; }
  }
  public string CallReceiveDate
  {
    get { return lblCallDateValue.Text; }
    set { lblCallDateValue.Text = value; }
  }
  public string ACModelNo
  {
    get { return txtACModelNo.Text; }
    set { txtACModelNo.Text = value; }
  }
  public int ACBrand
  {
    get { return int.Parse(ddlACBrands.SelectedValue); }
    set { ddlACBrands.SelectedValue = value.ToString(); }
  }
  public string Comment
  {
    get { return txtComment.Text; }
    set { lblPreviousComment.Text = value; }
  } 
  #endregion


}